package com.changanw.resilience4jboot;

import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import io.github.resilience4j.retry.annotation.Retry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
//@Retry(name = "retryA")//表示要使用的重试策略
@CircuitBreaker(name = "cbA",fallbackMethod = "error")
public class HelloService {
    @Autowired
    RestTemplate restTemplate;

    public String hello(){
        //模拟多次发送请求
        for (int i = 0; i < 10; i++) {
            restTemplate.getForObject("http://localhost:1113/hello",String.class);
        }
        return "success";
    }

    //降级请求
    public String error(Throwable t){
        return "error";
    }
}
